package org.xa3apm.vote.hibernate.bean;

import java.io.Serializable;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.xa3apm.vote.hibernate.entities.Candidate;
import org.xa3apm.vote.hibernate.entities.Municipality;
import org.xa3apm.vote.hibernate.entities.Settlement;
import org.xa3apm.vote.hibernate.entities.VoteType;

@Stateless
public class CandidateBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4133188134755860587L;

	@PersistenceContext(unitName = "vote")
	private EntityManager em;

	public Candidate getById(Long id) {
		return (Candidate) (em.createNamedQuery("Candidate.getById")
				.setParameter("id", id).getSingleResult());
	}

	public Collection<Candidate> getAll() {
		@SuppressWarnings("unchecked")
		Collection<Candidate> resultList = em.createNamedQuery(
				"Candidate.getAll").getResultList();
		return resultList;
	}

	public Collection<Candidate> getAllByType(VoteType type) {
		@SuppressWarnings("unchecked")
		Collection<Candidate> resultList = em
				.createNamedQuery("Candidate.getAllByType")
				.setParameter("type", type).getResultList();
		return resultList;
	}

	// @Override
	public Collection<Candidate> getAllBySettlement(Settlement settlement) {
		@SuppressWarnings("unchecked")
		Collection<Candidate> resultList = em
				.createNamedQuery("Candidate.getAllBySettlement")
				.setParameter("settlement", settlement).getResultList();
		return resultList;
	}

	public Collection<Candidate> getAllByMunicipality(Municipality municipality) {
		@SuppressWarnings("unchecked")
		Collection<Candidate> resultList = em
				.createNamedQuery("Candidate.getAllByMunicipality")
				.setParameter("municipality", municipality).getResultList();
		return resultList;
	}
}
